package com.situ.day6;

import org.junit.Test;

import java.util.Arrays;

public class StringDemo {
    // == 和 equals区别
    @Test
    public void test1() {
        // "abc"字符串常量，也是一个对象
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";
        //==比较的是引用指向的地址值
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//true
        System.out.println(str2 == str3);//false
        //equals比较的是内容
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        System.out.println(str2.equals(str3));//true
    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char ch = str.charAt(5);//A
        System.out.println(ch);
        System.out.println(str.indexOf('A'));//5
        System.out.println(str.indexOf('X'));//-1
        System.out.println(str.indexOf('A', 6));//12
        System.out.println(str.indexOf("And"));//5
        System.out.println(str.lastIndexOf('A'));//12
    }

    @Test
    public void test3() {
        String str1 = "java";
        String str2 = "Java";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true

        String str = "java AndroidA";
        System.out.println(str.contains("And"));//true
        System.out.println(str.startsWith("java"));//true
        //offset:偏移
        System.out.println(str.startsWith("And", 5));//true
        System.out.println(str.endsWith("oidA"));//true
        System.out.println(str.isEmpty());//false
        // ""空字符串，也是一个对象
        System.out.println("".isEmpty());//true
    }

    @Test
    public void test44() {
        // ""
        //String str = "java And";
        char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
        String str = new String(array);
        System.out.println(str);//java And
        char[] chars = str.toCharArray();
        System.out.println(chars);
        //Arrays：操作数组的工具类
        System.out.println(Arrays.toString(chars));
        //[j, a, v, a,  , A, n, d]
    }

    @Test
    public void test424() {
        String str = "java AndroidA";
        String newStr = str.replace('A', 'B');
        System.out.println(newStr);
    }

    @Test
    public void test66() {
        String str = "java android python";
        String[] array = str.split(" ");
        System.out.println(Arrays.toString(array));
    }

    @Test
    public void test87() {
        // subtree: 子树
        // substring:子字符串
        String str = "java AndroidA";
        System.out.println(str.substring(5));//AndroidA

        //beginIndex – the beginning index, inclusive.
        //endIndex – the ending index, exclusive.
        System.out.println(str.substring(5, 8));//Andr
    }

    @Test
    public void test77() {
        String str = "     Java Andorid     ";
        System.out.println(str);
        System.out.println(str.trim());
    }

    @Test
    public void test55() {
        String str1 = "java";
        String str2 = "php";
        String str3 = "python";
        StringBuilder builder = new StringBuilder("C");
        //builder.append(str1);
        //builder.append(str2);
        //builder.append(str3);
        builder.append(str1).append(str2).append(str3);

        StringBuffer buffer = new StringBuffer("C");
        buffer.append(str1).append(str2).append(str3);

        System.out.println("------------------");
        String str = builder.toString();
        System.out.println(str);
    }
}
